package com.woniuxy.school.online.comment.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.woniuxy.school.online.comment.entity.RepairNotice;
import com.woniuxy.school.online.comment.from.AddNoticeForm;
import com.woniuxy.school.online.comment.from.DelectNoticeForm;
import com.woniuxy.school.online.comment.from.UpdateNoticeForm;
import com.woniuxy.school.online.comment.mapper.RepairNoticeMapper;
import com.woniuxy.school.online.comment.service.RepairNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 通知公告 服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2025年05月14日
 */
@Service
public class RepairNoticeServiceImpl extends ServiceImpl<RepairNoticeMapper, RepairNotice> implements RepairNoticeService {
    @Autowired
    private RepairNoticeMapper repairNoticeMapper;


    @Override
    public void addNotice(AddNoticeForm addNoticeForm, String userId) {
        RepairNotice repairNotice = new RepairNotice();
        //生成唯一id
        Snowflake snowflake = new Snowflake(1, 1);
        repairNotice.setId(snowflake.nextId());
        repairNotice.setNoticeTitle(addNoticeForm.getNoticeTitle());
        repairNotice.setNoticeContent(addNoticeForm.getNoticeContent());
        repairNotice.setPublisher(Long.valueOf(userId));
        repairNotice.setReleaseDate(LocalDateTime.now());
        repairNotice.setBrowseTimes(0);
        repairNotice.setDeleted(1);
        repairNoticeMapper.insert(repairNotice);
    }

    @Override
    public void deleteNotice(DelectNoticeForm delectNoticeForm, String userId) {
        repairNoticeMapper.deleteById(delectNoticeForm.getId());
    }

    @Override
    public void updateNotice(UpdateNoticeForm updateNoticeForm, String userId) {
        RepairNotice repairNotice = repairNoticeMapper.selectById(updateNoticeForm.getId());
        repairNotice.setNoticeTitle(updateNoticeForm.getNoticeTitle());
        repairNotice.setNoticeContent(updateNoticeForm.getNoticeContent());
        repairNotice.setReleaseDate(LocalDateTime.now());
        repairNotice.setPublisher(Long.valueOf(userId));
        repairNoticeMapper.updateById(repairNotice);
    }

    @Override
    public List<RepairNotice> selectNotice() {
        List<RepairNotice> list = repairNoticeMapper.selectList(null);
        return list;
    }
}
